package my_lib

import (
	. "github.com/henrylee2cn/pholcus/app/spider"           //必需
	"github.com/henrylee2cn/pholcus/app/downloader/request"
	"encoding/json"
	"log"
	"math"
	"fmt"
)


//采集的url 入口地址
var http_url = "http://lvyou.baidu.com/destination/ajax/jingdian?format=ajax&cid=0&playid=0&surl=zhongguo"
//var http_url = "http://lvyou.baidu.com/destination/ajax/jingdian?format=ajax&cid=0&playid=0&surl=qingwanzai";

var fomart_url = "http://lvyou.baidu.com/destination/ajax/jingdian?format=ajax&cid=0&playid=0&surl=%s&rn=%d&pn=%d"

const pageSize = 18

type  ResPonse_json struct {
	Errno int  `json:"errno"`     //状态码
	Msg   string `json:"msg"`     //信息说明
	Data  Data_json `json:"data"` //数据内容
}
//返回的data 数据
type Data_json struct {
	Scene
	Context     interface{} `json:"content"`
	Scene_total int `json:"scene_total"`    //景区数量
	Scene_list  []Scene `json:"scene_list"` //景区数量
}

type Besttime struct {
	Simple_desc          string `json:"simple_desc"`
	More_desc            string `json:"more_desc"`
	Recommend_visit_time string `json:"recommend_visit_time"`
}
//attention


//景区
type Scene struct {
	Sid            string `json:"sid"`            //景区id
	Surl           string `json:"surl"`           //景区url
	Parent_sid     string  `json:"parent_sid"`    //父级景区ID
	Cid            string `json:"cid"`            //栏目ID
	Scene_layer    string `json:"scene_layer"`    //栏目层级
	Place_uid      string `json:"place_uid"`      //地址uid
	Place_name     string `json:"place_name"`     //地址名称
	Qunar_code     string `json:"qunar_code"`     //去哪code
	Sname          string `json:"sname"`          //景区名称
	Star           string  `json:"star"`          //景区星别
	Ambiguity_name string `json:"ambiguity_name"` //模糊名称
	Ext            ExtData  `json:"ext"`          //扩展字段
	Going_count    int64  `json:"going_count"`
	Gone_count     int64 `json:"gone_count"`
}

//景区拓展内容
type ExtData struct {
	Sid              string `json:"sid"`         //景区iD
	Lower_desc       string `json:"lower_desc"`
	Lower_count      string `json:"lower_count"`
	Scene_layer      string `json:"scene_layer"`
	Map_info         string `json:"map_info"`
	Md5              string  `json:"md5"`
	Poid             string   `json:"poid"`
	Alias            string  `json:"alias"`
	Impression       string `json:"impression"`
	En_name          string `json:"en_name"`
	Address          string `json:"address"`
	Phone            string `json:"phone"`
	Level            string `json:"level"`
	WebSite          string `json:"website"`
	Abs_desc         string `json:"abs_desc"`
	More_desc        string `json:"more_desc"`
	Avg_remark_score string `json:"avg_remark_score"`
	Avg_cost         string   `json:"avg_const"` //平均花费
}

func init() {
	BaiduLvSpider.Register()
}

//百度旅游兴趣点采集
var BaiduLvSpider = &Spider{
	Name:"百度旅游兴趣点采集",
	Description:"beta1 采集景点数据",
	EnableCookie:false,
	RuleTree:&RuleTree{
		//根入口
		Root:func(ctx *Context) {

			ctx.AddQueue(&request.Request{
				Url:http_url,
				Rule:"请求页",
			})
		},
		//树干
		Trunk:map[string]*Rule{
			"请求页":{
				ItemFields:[]string{
					"sid",
					"surl",
					"parent_sid",
					"cid",
					"scene_layer",
					"place_uid",
					"place_name",
					"sname",
					"star",
					"ambiguty_name",
					"lower_desc",
					"lower_count",
					"map_info",
					"going_count",
					"gone_count",
					"poid",
					"alias",
					"en_name",
					"address",
					"phone",
					"level",
					"website",
					"abs_desc",
					"more_desc",
					"impression",
					"avg_remark_score",
					"avg_cost",
					"best_time_simple",
					"best_time_desc",
					"recommend_visit_time",
					"price_desc",
					"open_time_desc",
					"qunar_code", //去哪cod

				},
				ParseFunc:func(ctx *Context) {
					if ctx.GetError() != nil {
						log.Panic("首页的json内容获取error")
					}
					vdata := ResPonse_json{}
					err := json.Unmarshal([]byte(ctx.GetText()), &vdata)
					if err != nil {
						log.Panic(err)
					}
					var best_simple, best_desc, best_visit, price_desc, open_time_desc interface{}
					//contents := vdata.Data.Context
					//断言内容
					contents := vdata.Data.Context
					if contents_v, ok := contents.(map[string]interface{}); ok {
						if v, found := contents_v["besttime"]; found {
							//fmt.Println(vdata.Data.Context)
							if best, ok := v.(map[string]interface{}); ok {
								best_simple = best["simple_desc"]
								best_desc = best["more_desc"]
								best_visit = best["recommend_visit_time"]
							}

						}

						//获取票价信息
						if v, found := contents_v["ticket_info"]; found {
							if best, ok := v.(map[string]interface{}); ok {
								price_desc = best["price_desc"]
								open_time_desc = best["open_time_desc"]
							}
						}
					}

					out := vdata.Data
					ctx.Output(map[int]interface{}{
						0:out.Sid,
						1:out.Surl,
						2:out.Parent_sid,
						3:out.Cid,
						4:out.Scene_layer,
						5:out.Place_uid,
						6:out.Place_name,
						7:out.Sname,
						8:out.Star,
						9:out.Ambiguity_name,
						10:out.Ext.Lower_desc,
						11:out.Ext.Lower_count,
						12:out.Ext.Map_info,
						13:out.Going_count,
						14:out.Gone_count,
						15:out.Ext.Poid,
						16:out.Ext.Alias,
						17:out.Ext.En_name,
						18:out.Ext.Address,
						19:out.Ext.Phone,
						20:out.Ext.Level,
						21:out.Ext.WebSite,
						22:out.Ext.Abs_desc,
						23:out.Ext.More_desc,
						24:out.Ext.Impression,
						25:out.Ext.Avg_remark_score,
						26:out.Ext.Avg_cost,
						27:best_simple,
						28:best_desc,
						29:best_visit,
						30:price_desc,
						31:open_time_desc,
						32:out.Qunar_code,
					})


					//拥有子景区
					if (out.Scene_total > 0) {
						//分页总数
						pageTotal := getPage(vdata.Data.Scene_total)
						for i := 1; i <= int(pageTotal); i++ {
							ctx.AddQueue(&request.Request{
								Url:fmt.Sprintf(fomart_url, out.Surl, pageSize, i),
								Rule:"解析内容",
							})
						}
					}

				},
			},
			"解析内容":{

				ParseFunc:func(ctx *Context) {
					if ctx.GetError() != nil {
						log.Panic("首页的json内容获取error")
					}
					var _http_url = "http://lvyou.baidu.com/destination/ajax/jingdian?format=ajax&cid=0&playid=0&surl=";
					vdata := ResPonse_json{}
					err := json.Unmarshal([]byte(ctx.GetText()), &vdata)
					if err != nil {
						log.Panic(err)
					}

					for _, out := range vdata.Data.Scene_list {
						ctx.AddQueue(&request.Request{
							Url:_http_url + out.Surl,
							Rule:"请求页",
						})
					}
				},

			},

		},

	},

}

//获取分页的数量
func getPage(num int) int {
	a := math.Ceil(float64(num / pageSize))
	return int(a)
}